<?php
    session_start();
    include_once '../../../../controlador/php/conexion/Conexion.php';
    $con = new Conexion();
    
    $oid = $_REQUEST['oid'];
    
    $count = "select count(*) as n, segobserva,segfecobs,usu_oidobs from segimagenes 
              where hcnsolexa = ".$oid." and segobserva is not null and 
					segfecobs is not null and segfecobs is not null and
					usu_oidobs is not null
              GROUP BY segobserva,segobserva,segfecobs,usu_oidobs";
    $res  = $con ->query($count);
    
    while($res2 = mssql_fetch_array($res)){
       $n = $res2['n'];
        $obs = $res2['segobserva'];
        $fec = $res2['segfecobs'];
        $usu = $res2['usu_oidobs'];
    }
    if($n == 1){
        
        $existe = "select COUNT(*) as num from segobservacion
                   where segobserva = '".$obs."' and usu_oid = '".$usu."' and hcnsolexa = '".$oid."'";
        $resp = $con ->query($existe);
        
        while($resp2 = mssql_fetch_array($resp)){
            $num = $resp2['num'];
        }
         if($num != 1){
        $inser = "insert into segobservacion(hcnsolexa,segobserva,segfecha,usu_oid)values('".$oid."','".$obs."','".$fec."','".$usu."')";
        $con->query($inser);
    }
    }
    
    $select ="select oid,hcnsolexa,cast(segobserva as text)as segobserva,segfecha,ltrim(rtrim(usu_nombres++usu_apellidos ))as usunombres
             from segobservacion
             inner join usuario on segobservacion.usu_oid =  usuario.usu_oid
             where hcnsolexa = '".$oid."'
			 order by segfecha desc";
    $respu = $con->query($select);
    
    while
        ($re = mssql_fetch_array($respu)){
        $lista[] = array_map(utf8_encode,$re);
    }
    
     print_r(json_encode($lista));
    $con->cerrarConexion();     
    
 
?>